package cn.chia.back.action;

import java.text.SimpleDateFormat;
import java.util.Date;
import java.util.List;
import java.util.Map;

import javax.annotation.Resource;

import org.apache.struts2.ServletActionContext;
import org.springframework.stereotype.Controller;

import cn.chia.pojo.TblActivity;
import cn.chia.pojo.TblActivityType;
import cn.chia.pojo.TblItems;
import cn.chia.service.IActivityService;
import cn.chia.service.IItemService;
import cn.chia.utils.BuildLogFile;
import cn.chia.utils.OnCustomerService;
import cn.chia.utils.Onmanager;

import com.opensymphony.xwork2.ActionSupport;

@Controller
public class ActivityAction extends ActionSupport{
	private IActivityService activityServiceImpl;
	private IItemService itemServiceImpl;
	
	private TblActivity tblActivity;
	private TblItems tblItems;
	
	private List<TblActivityType> typeList;
	private List<?> itemsList;
	private List<?> activityList;
	
	private Integer activId;
	private String activityName;
	
	// 分页参数
	private int pageNo = 1;
	private int pageSize = 5;
	private String keyword = "";
	private String column = "activityName";
	
	private String itemsColumn = "itemName";

	// 列表数据结果集参数和全部记录数参数
	private Integer count;
	
	/**
	 * @Todo 分页显示全部活动信息
	 * @author 徐艳慧
	 * @throws Exception
	 */
	public String findAllActivity()throws Exception{
			Map<String, Object> map = (Map<String, Object>) activityServiceImpl.findAllActivity(pageNo, pageSize, keyword, column);
			activityList = (List<?>) map.get("allActivity");
			count = (Integer) map.get("allActivityCount");
			return "findAllActivity";
		}
	
	/**
	 * @Todo 添加活动之前，获取全部的活动类型，传值给页面上的下拉列表
	 * @author 徐艳慧
	 * @throws Exception
	 */
	public String preAddActivity()throws Exception{
		typeList = activityServiceImpl.findAllActivityType();
		return "preAddActivity";
	}
	
	/**
	 * @author 徐艳慧
	 * @Todo 添加新活动
	 * @throws Exception
	 */
	public String addActivity()throws Exception{
		tblActivity.setStartTime(tblActivity.getStartTime().replace("T"," "));
		tblActivity.setEndTime(tblActivity.getEndTime().replace("T"," "));
		
		activityServiceImpl.addActivity(tblActivity);
		//生成日志
		BuildLogFile.build(OnCustomerService.customerService().getCsname()+"于"+new SimpleDateFormat("yyyy-MM-dd HH:mm:ss").format(new Date())+"增加了一个新活动："+tblActivity.getActivityName()+"。\r\n");
		return "addActivity";
	}
	
	/**
	 * @Todo 修改活动之前，将活动信息获取到修改页面上。
	 * @author 徐艳慧
	 * @throws Exception
	 */
	public String preModify()throws Exception{
		tblActivity = activityServiceImpl.getActivityById(tblActivity.getActivityId());
		typeList = activityServiceImpl.findAllActivityType();
		return "preModify";
	}
	
	/**
	 * @Todo 修改活动
	 * @author 徐艳慧
	 */
	public String modifyActivity() throws Exception{
		ServletActionContext.getRequest().getParameter("activityName");
		tblActivity.setStartTime(tblActivity.getStartTime().replace("T"," "));
		tblActivity.setEndTime(tblActivity.getEndTime().replace("T"," "));
		tblActivity.setActivityName(activityName);
		activityServiceImpl.modifyActivity(tblActivity);
		//生成日志
		BuildLogFile.build(OnCustomerService.customerService().getCsname()+"于"+new SimpleDateFormat("yyyy-MM-dd HH:mm:ss").format(new Date())+"修改了一个新活动："+tblActivity.getActivityName()+"。\r\n");
		return "modifyActivity";
	}
	

	/**
	 * @Todo 根据二级分类查询商品。
	 * @author 徐艳慧
	 * @throws Exception 
	 */
	public String findAllItems() throws Exception{

		Map<String, Object> map = (Map<String, Object>)itemServiceImpl.findAll(pageNo, pageSize, keyword, itemsColumn);
		//分页显示所有商品
		itemsList = (List<?>) map.get("allItem");
		
		count = (Integer) map.get("allCount");
		if(activId != null){
			ServletActionContext.getRequest().getSession().setAttribute("actId", activId);
		}
		
		return "findAllItems";
	}
	
	/**
	 * @Todo 给某一个商品添加新的活动
	 * @author 徐艳慧
	 * @throws Exception 
	 */
	public String addActivityInItems() throws Exception{
		Integer actId = (Integer) ServletActionContext.getRequest().getSession().getAttribute("actId");
		activityServiceImpl.modifyActivityInItems(tblItems.getItemId(),actId);
		return "addActivityInItems";
	}
	
	/**
	 * @Todo 取消某件正在商品参加的活动
	 * @author 徐艳慧
	 * @throws Exception
	 */
	public String removeActivityInItems() throws Exception{
		activityServiceImpl.modifyActivityInItems(tblItems.getItemId(),1);
		return "removeActivityInItems";
	}
	

	public int getPageNo() {
		return pageNo;
	}
	public void setPageNo(int pageNo) {
		this.pageNo = pageNo;
	}
	public int getPageSize() {
		return pageSize;
	}
	public void setPageSize(int pageSize) {
		this.pageSize = pageSize;
	}
	public String getKeyword() {
		return keyword;
	}
	public void setKeyword(String keyword) {
		this.keyword = keyword;
	}
	public String getColumn() {
		return column;
	}
	public void setColumn(String column) {
		this.column = column;
	}
	public List<?> getActivityList() {
		return activityList;
	}
	public void setActivityList(List<?> activityList) {
		this.activityList = activityList;
	}
	
	
	
	public List<TblActivityType> getTypeList() {
		return typeList;
	}

	public void setTypeList(List<TblActivityType> typeList) {
		this.typeList = typeList;
	}

	

	public TblActivity getTblActivity() {
		return tblActivity;
	}

	public void setTblActivity(TblActivity tblActivity) {
		this.tblActivity = tblActivity;
	}

	


	public Integer getCount() {
		return count;
	}

	public void setCount(Integer count) {
		this.count = count;
	}

	public List<?> getItemsList() {
		return itemsList;
	}

	public void setItemsList(List<?> itemsList) {
		this.itemsList = itemsList;
	}

	

	public String getItemsColumn() {
		return itemsColumn;
	}

	public void setItemsColumn(String itemsColumn) {
		this.itemsColumn = itemsColumn;
	}


	public TblItems getTblItems() {
		return tblItems;
	}

	public void setTblItems(TblItems tblItems) {
		this.tblItems = tblItems;
	}

	
	
	public Integer getActivId() {
		return activId;
	}

	public void setActivId(Integer activId) {
		this.activId = activId;
	}

	@Resource(name="activityServiceImpl")
	public void setActivityServiceImpl(IActivityService activityServiceImpl) {
		this.activityServiceImpl = activityServiceImpl;
	}
	

	@Resource(name="itemServiceImpl")
	public void setItemServiceImpl(IItemService itemServiceImpl) {
		this.itemServiceImpl = itemServiceImpl;
	}

	public String getActivityName() {
		return activityName;
	}

	public void setActivityName(String activityName) {
		this.activityName = activityName;
	}
	
	
	
}
